﻿using System;
using System.Collections;
using System.IO;
using UnityEngine;

namespace TTGame {

/// <summary>
// LTPlayerPrefs
// Created by tzy - 2018.4 - 用于玩家报错时，上传存档，方便排查错误
/// </summary>
public class LTPlayerPrefs : Framework.Singleton<LTPlayerPrefs> {

    private Hashtable m_table = new Hashtable();

    //
    // Summary:
    //     Removes key and its corresponding value from the preferences.
    //
    public void DeleteKey(string key) {
        m_table[key] = null;
    }

    //
    // Summary:
    //     Returns true if key exists in the preferences.
    //
    public bool HasKey(string key) {
        return m_table[key] != null;
    }

    //
    // Summary:
    //     Returns the value corresponding to key in the preference file if it exists.
    //
    public int GetInt(string key) {
        return GetInt(key, 0);
    }

    //
    // Summary:
    //     Returns the value corresponding to key in the preference file if it exists.
    //
    public int GetInt(string key, int defaultValue) {
        if(m_table[key] == null)
            return defaultValue;
        var val = m_table[key].ToString();
        int ret = 0;
        if(int.TryParse(val, out ret)) {
            return ret;
        }
        return defaultValue;
    }

    //
    // Summary:
    //     Sets the value of the preference identified by key.
    //
    public void SetInt(string key, int value) {
        if(!string.IsNullOrEmpty(key))
            m_table[key] = value;
    }

    //
    // Summary:
    //     Returns the value corresponding to key in the preference file if it exists.
    //
    public string GetString(string key) {
        return GetString(key, string.Empty);
    }

    //
    // Summary:
    //     Returns the value corresponding to key in the preference file if it exists.
    //
    public string GetString(string key, string defaultValue) {
        if(m_table[key] == null)
            return defaultValue;
        return m_table[key].ToString();
    }

    //
    // Summary:
    //     Sets the value of the preference identified by key.
    //
    public void SetString(string key, string value) {
        if(!string.IsNullOrEmpty(key))
            m_table[key] = value;
    }

    protected override void Initialize() {
        base.Initialize();
        this.Load();
    }

    private void Load() {
        if(PlayerPrefs.HasKey(LTRecordKey.Data)) {
            try {
                var context = PlayerPrefs.GetString(LTRecordKey.Data);
                string json = EncryptUtils.Decrypt(WWW.UnEscapeURL(context));
                if(!string.IsNullOrEmpty(json)) {
                    m_table = MiniJSON.jsonDecode(json) as Hashtable;
                } else {
                    PlayerPrefs.DeleteKey(LTRecordKey.Data);
                    PlayerPrefs.Save();
                }
            } catch(Exception e) {
                PlayerPrefs.DeleteKey(LTRecordKey.Data);
                PlayerPrefs.Save();
            }
        }

        if(null == m_table) {
            m_table = new Hashtable();
        }
    }

    //
    // Summary:
    //     Writes all modified preferences to disk.
    public void Save() {
        PlayerPrefs.SetString(LTRecordKey.Data, this.ToString());
        PlayerPrefs.Save();
    }

    // Summary:
    //     Removes all keys and values from the preferences. Use with caution.
    public void DeleteAll() {
        m_table.Clear();
        this.Save();
    }

    public override string ToString() {
        if(null == m_table)
            return string.Empty;

        string json = MiniJSON.jsonEncode(m_table);
        string encryptJson = EncryptUtils.Encrypt(json);
        string result = WWW.EscapeURL(encryptJson);
        return result;
    }
}
}
